package com.cn.wang.shiro;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.cn.wang.entry.user;
import com.cn.wang.service.impl.UserServiceImpl;

public class MyShiroRealm extends AuthorizingRealm{
	@Autowired
  private UserServiceImpl userServiceImpl;
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		String name = token.getUsername();
		char[] pwd = token.getPassword();
		String pwds =pwd.toString();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("pswd", pwds);
		user user = userServiceImpl.login(map);
		if(null == user){
			throw new AccountException("帐号或密码不正确！");
		}
		return new SimpleAuthenticationInfo(user,user.getPassword(), getName());
	}

}
